package com.ha.garbage;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ha.garbage.context.BaseContext;
import com.ha.garbage.model.Result;
import com.ha.garbage.model.UserLoginVO;
import com.ha.garbage.model.request.LoginRequest;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        EditText account = findViewById(R.id.editTextTextAccount);
        EditText password = findViewById(R.id.editTextTextPassword);
        //游客登录
        findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  new Intent(MainActivity.this,MenuActivity.class);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("登录方式")
                        .setMessage("当前为游客方式登录")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(intent);

                            }
                        })
                        .show();

            }
        });
        //账号密码登录
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =  new Intent(MainActivity.this,MenuActivity.class);
                Bundle bundle = new Bundle();
                LoginRequest request = new LoginRequest(account.getText().toString(), password.getText().toString());

                Retrofit retrofit = UserRetrofit.retrofit();
                userService userService = retrofit.create(userService.class);
                Call<Result<UserLoginVO>> login = userService.Login(request);
                login.enqueue(new Callback<Result<UserLoginVO>>() {
                    @Override
                    public void onResponse(Call<Result<UserLoginVO>> call, Response<Result<UserLoginVO>> response) {
                        System.out.println("进入response");
                        if(response.isSuccessful()){
                            Result<UserLoginVO> body = response.body();
                            if(body.getCode()  == 1){
                                System.out.println("请求成功");
                                BaseContext.setCurrentId(body.getData().getId());
                                BaseContext.setCurrentName(body.getData().getName());
                                BaseContext.setCurrentCreditPoint(body.getData().getCreditPoint());
                                BaseContext.setCurrentToken(body.getData().getToken());
                                System.out.println(BaseContext.getCurrentId());
                                bundle.putString("token",body.getData().getToken());
                                System.out.println(body.getData().getToken());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }else{
                                System.out.println("请求出错");
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("登录失败")
                                        .setMessage("账号或密码错误")
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {

                                            }
                                        })
                                        .show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<UserLoginVO>> call, Throwable t) {

                        System.out.println("请求路径为："+call.request().url().toString()+"请求失败");


                        System.out.println(t.toString());
                    }
                });


            }
        });
    }


}